package cc.yaomm.common.basic.enums;

import cc.yaomm.common.basic.base.SuperEnum;
import com.google.common.base.Joiner;
import cc.yaomm.common.basic.constant.DefaultConstant;
import cc.yaomm.common.basic.exception.BusinessException;
import lombok.Getter;
import org.springframework.util.StringUtils;

import java.io.File;

/**
 * Created by XQL on 2020/5/20.
 * 注意事项：在新增目录时，考虑哈是否可以用现有的目录名称（不要随意新增），尽量少新增目录，多了容易杂乱。
 */
@Getter
public enum OSSDirectory implements SuperEnum<OSSDirectory, String> {

    CERTIFICATE("certificate", "药店/商家/商品资质"),
    VIPPROTOCOL("vipprotocol", "vip客户协议"),
    ORDINARYCONTROL("ordinarycontrol", "控销图片"),
    CORPORATE("corporate", "对公打款"),
    VOUCHER("voucher", "打款凭证"),
    SCHOOL("school", "商学院"),
    PREFECTURE("prefecture", "主题专区"),
    LUCKYDRAWACTIVITIES("luckyDrawActivities", "幸运抽奖活动"),
    SUPPLY("supply", "商家图片"),
    CONTRACT("contract", "商家合同"),
    PROBLEM("problem", "自学院在线提问"),
    EXPRESS("express", "快递单"),
    DECORATE("decorate", "商家装饰"),
    APK("apk", "APP包"),
    APPCONFIG("appconfig", "APP配置图片"),
    BANNER("banner", "banner图"),
    ARTICLE("article", "资讯文章"),
    RECORD("record", "跟踪药店记录"),
    UEDITOR("ueditor", "富文本资源"),
    TEMP("temp", "临时目录，会定时清理"),
    IM("im", "IM聊天"),
    IMPORT("import", "导入excel"),
    SERVICEFEE("servicefee", "商家服务费凭证"),
    PRODUCT("product", "商品图片"),
    ADVERT("advert", "广告图"),
    ORDERINVOICE("orderinvoice", "订单发票"),
    REFUND("refund", "订单退款图片");
    /**
     * 注意事项：在新增目录时，考虑哈是否可以用现有的目录名称（不要随意新增），尽量少新增目录，多了容易杂乱。
     */
    //注意事项：在新增目录时，考虑哈是否可以用现有的目录名称（不要随意新增），尽量少新增目录，多了容易杂乱。

    private String value;

    private String name;

    OSSDirectory(String value, String name) {
        this.value = value;
        this.name = name;
    }

    public String getValue() {
        String env = System.getProperty(DefaultConstant.SPRING_PROFILES_ACTIVE);
        env = StringUtils.hasText(env) ? env : "test";
        return Joiner.on(File.separator).join(env, value);
    }

    @Override
    public OSSDirectory[] enums() {
        return values();
    }

    public static OSSDirectory getOSSDirectory(String path) {
        for (OSSDirectory ossDirectory : OSSDirectory.values()) {
            //根据路径名匹配
            if (ossDirectory.value.equalsIgnoreCase(path)) {
                return ossDirectory;
            }
            //根据枚举名匹配，兼容老目录指向新目录
            if (ossDirectory.name().equalsIgnoreCase(path)) {
                return ossDirectory;
            }
        }
        throw new BusinessException("不允许上传此类型的文件资源");
    }
}
